﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace XBoshiGFS.forms
{
    public partial class RePassWordForm : Form
    {
        public RePassWordForm()
        {
            InitializeComponent();
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            if (TBNewPassWord.Text =="")
            {
                MessageBox.Show("新密码不能为空。", "提示");
                return;
            }

            if (TBNewPassWord.Text != TBConfirmPassWord.Text) {
                MessageBox.Show("2次密码输入不一致。", "提示");
                return;
            }

            String currentUser = MainMDIParent.currentUserCode;

            using(XBoshiGFSModelContainer context=new XBoshiGFSModelContainer() ){
                var user=from p in context.UsersSet where p.strCode ==currentUser select p; 
                var u=user.FirstOrDefault ();
                if (u == null) {
                    MessageBox.Show("当前登录信息丢失，请重新登录！", "提示");
                    this.Close();
                    Application.Exit () ;
                    return;
                }
                if (TBOldPassWord.Text != u.strPassWord)
                {
                    MessageBox.Show("旧密码不正确！", "提示");
                    return;
                }
                else {
                    u.strPassWord = TBNewPassWord.Text;
                    context.SaveChanges();
                    this.Close();
                }    
            
            }

        }
    }
}
